﻿/* Write a program that reads a text file containing a square matrix of numbers and finds in the matrix an area of size 2 x 2 with a maximal sum of its elements. The first line in the input file contains the size of matrix N. Each of the next N lines contain N numbers separated by space. The output should be a single number in a separate text file. Example:
4
2 3 3 4
0 2 3 4		->	17
3 7 1 2
4 3 3 2
*/
using System;
using System.IO;

namespace _5.SquareMatrix
{
    class SquareMatrix
    {
        static void Main()
        {
            Console.Write("Path to input file (the file containning the matrix): ");
            string path_to_input_file = Console.ReadLine();

            Console.Write("Path to output file (the file to which will be written the output result): ");
            string path_to_output_file = Console.ReadLine();

            StreamReader SR = new StreamReader(@path_to_input_file);

            string line = "";
            string number = "";
            int sizeOfMatrix;
            int row = 0;
            int col = 0;

            using (SR)
            {
                //read the matrix and allocate space for it
                line = SR.ReadLine();
                sizeOfMatrix = int.Parse(line);
                int[,] matrix = new int[sizeOfMatrix, sizeOfMatrix];

                while ((line = SR.ReadLine()) != null)
                {
                    //fill the matrix
                    foreach (var symbol in line)
                    {
                        if (symbol != ' ')
                        {
                            number += symbol;
                        }
                        else
                        {
                            matrix[row, col] = int.Parse(number);
                            number = "";
                            col++;
                        }
                    }
                    matrix[row, col] = int.Parse(number);
                    col = 0;
                    row++;
                    number = "";
                }

                //write the maximal sum to the output file
                try
                {
                    StreamWriter SW = new StreamWriter(@path_to_output_file);
                    using (SW)
                    {
                        SW.WriteLine(MaxS(matrix));
                    }
                }
                catch (FileNotFoundException)
                {
                    throw new FileNotFoundException("File not found in the specified directory or the specified file does not exist");
                }
                catch (IOException ioe)
                {
                    throw new IOException("Error: could not write data to file:\r\n" + ioe.Message);
                }
                Console.WriteLine("Data successfully written to {0}", path_to_output_file);
            }
        }
        private static int MaxS(int[,] matrix)
        {

            int bestSum = int.MinValue;
            int currentSum = 0;

            for (int row = 0; row < matrix.GetLength(0) - 1; row++)
            {
                for (int col = 0; col < matrix.GetLength(1) - 1; col++)
                {
                    currentSum = matrix[row, col] + matrix[row, col + 1] +
                                 matrix[row + 1, col] + matrix[row + 1, col + 1];
                    if (bestSum < currentSum)
                    {
                        bestSum = currentSum;
                    }
                    currentSum = 0;
                }
            }
            return bestSum;
        }
    }
}
